//
//  Manangedcharts.m
//  QAQ打飞机
//
//  Created by admin on 15/9/9.
//  Copyright (c) 2015年 gl. All rights reserved.
//

#import "Manangedcharts.h"
#import "Heoll.h"
#define MOC [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]
static NSString *sr = @"Heoll";

@implementation Manangedcharts

#pragma mark -插入新分数
+(void)insertNewScoreWith:(NSNumber *)score name:(NSString *)name
{
    @try {
        Heoll *s = [NSEntityDescription insertNewObjectForEntityForName:sr inManagedObjectContext:MOC];
        if (s != nil)
        {
            s.score = score;
            s.name = name;
            [MOC save:nil];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
}

#pragma mark -查找所有分数
+(NSArray *)selectedAllScore
{
    @try {
        NSArray *resArr = nil;
        
        NSFetchRequest *fr = [[NSFetchRequest alloc]init];
        NSEntityDescription *ed = [NSEntityDescription entityForName:sr inManagedObjectContext:MOC];
        [fr setEntity:ed];
        
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
        [fr setSortDescriptors:@[sort]];
        
        resArr = [MOC executeFetchRequest:fr error:nil];
        
        if (resArr.count > 8)
        {
            [MOC deleteObject:[resArr lastObject]];
            [MOC save:nil];
            resArr = [MOC executeFetchRequest:fr error:nil];
        }
        
        return resArr;
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
}



@end
